#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
 
#include "simnand.h"

int main (void) {
    int sock_fd;
    char buf[1024], sendbuf[1024], recvbuf[1024];
    struct sockaddr_in server_addr;
    int ret;

    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd == -1) {
        printf("%s\n", strerror(errno));
        return 0;
    }

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(DEFAULT_PORT);
    server_addr.sin_addr.s_addr = inet_addr(DEFAULT_IP);

    ret = connect(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr));
    if (ret < 0) {
        printf("%s\n", strerror(errno));
        return 0;
    }

    while (1) {
        fgets(sendbuf, sizeof(sendbuf), stdin);

        send(sock_fd, sendbuf, strlen(sendbuf), 0);
        if(strcmp(sendbuf, "exit\n") == 0)
            break;

        recv(sock_fd, recvbuf, sizeof(recvbuf), 0);
        fputs(recvbuf, stdout);

        memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));
    }

    close(sock_fd);

    return 0;
}
